package com.jingbit.ai.service;

import android.content.Intent;
import android.os.IBinder;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.jingbit.ai.baidu.map.BDConstants;
import com.jingbit.ai.baidu.map.BaiduMap;
import com.jingbit.ai.base.BaseService;
import com.jingbit.ai.helper.Logger;
import com.jingbit.ai.weather.HFWeather;
import com.jingbit.ai.weather.XZWeather;
import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.bean.base.Unit;

import java.util.ArrayList;
import java.util.List;

public class LocationService extends BaseService {

	public static List<BDLocationListener> listeners=new ArrayList<>();

	@Override
	public IBinder onBind(Intent arg0) {
		return super.onBind(arg0);
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		initBaiduService();
//		initAlarmReceiver();
		return START_STICKY;
	}

	private void initBaiduService() {
		BDAbstractLocationListener bdAbstractLocationListener=new BDAbstractLocationListener() {
			@Override
			public void onReceiveLocation(BDLocation location) {
				if (null != location && location.getLocType() != BDLocation.TypeServerError) {
					Logger.log(this, "onReceiveLocation suc", location.getLocationID(),location.getCity(), location.getAddrStr());
					BDConstants.location = location;
					for (BDLocationListener listener:listeners){
						if (listener!=null){
							listener.onReceiveLocation(location);
						}
					}
					HFWeather.getInstance().getWeather(getApplicationContext(),0,location.getLongitude()+","+location.getLatitude(),Lang.ZH_HANS, Unit.METRIC);
//					XZWeather.getInstance().getWeather("TY8G4T7ZWVX1", Lang.ZH_HANS,"c","0",0);
				}
			}
		};
		BaiduMap.registerListener(bdAbstractLocationListener, 15*1000);
		BaiduMap.start();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		BaiduMap.stop();
		listeners.clear();
	}

	@Override
	public void onTrimMemory(int level) {
		super.onTrimMemory(level);
	}

}
